<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Home\UserInfo;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;

class UserInfoController extends Controller
{
    /**
     * 用户中心展示
     */
    public function user_info()
    {
        //分配session信息到模板
        $user_info= session('sign_user_info');
        $member_info = DB::table('user_info')->where(['user_id'=>$user_info['id']])->get();
        return view('Home/User/user_info')->with('user_info',$user_info)->with('member_info',$member_info);
    }
    /**
     * 添加用户信息
     */
    public function user_info_add(Request $request)
    {
        $user_info= session('sign_user_info');
        if(empty($user_info['id']))
        {
            return ['status'=>0,'info'=>'请登录后修改个人信息'];
        }
        //获取ajax发送过来的用户信息
        $data = $request->input();
        //验证用户名信息
        if(empty($data['realname']) || mb_strlen($data['realname'])<1 || mb_strlen($data['realname'])>11 )
        {
            return ['status'=>0,'info'=>'真实姓名信息填写有误'];
        }
        //验证用户性别信息
//        if($data['sex'] != 0 || $data['sex'] != 1)
//        {
//            return ['status'=>0,'info'=>'性别信息填写有误'];
//        }
        //邮箱信息验证
        preg_match('/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i',$data['email'],$match);
        if(!$match)
        {
            return ['status'=>0,'info'=>'邮箱信息填写有误'];
        }
        //验证身份证信息
        $userInfo = new UserInfo();
        $res = $userInfo->id_card_available($data['idcard']);
        if(!$res)
        {
            return ['status'=>0,'info'=>'身份证信息填写有误'];
        }
        //to do判断该用户是新增信息 还是修改信息 先从数据库查询后作出判断
        $res1 = DB::table('user_info')->where(['user_id'=>$user_info['id']])->first();
        if($res1)
        {
            //修改用户信息
            $info_update = DB::table('user_info')->where(['user_id'=>$user_info['id']])->update([
                'idcard'=>$data['idcard'],
                'sex'=>$data['sex'],
                'real_name'=>$data['realname'],
                'email'=>$data['email']
            ]);
            if($info_update)
            {
                return ['status'=>1,'info'=>'个人信息修改成功'];
            }else{
                return ['status'=>0,'info'=>'个人信息修改失败'];
            }
        }else{
            //添加用户信息
            $info_add = DB::table('user_info')->insert([
               'user_id'=>$user_info['id'],
                'idcard'=>$data['idcard'],
                'sex'=>$data['sex'],
                'real_name'=>$data['realname'],
                'email'=>$data['email']
            ]);
            if($info_add)
            {
                return ['status'=>1,'info'=>'个人信息添加成功'];
            }else{
                return ['status'=>0,'info'=>'个人信息添加失败'];
            }
        }


    }

    /**
     * 用户信息列表
     */
    public function user_info_list()
    {

    }

    /**
     * 用户收货地址添加
     */
    public function user_address(Request $request)
    {
        $res = $request->method();
        //分配session信息到主页模板
        $user_info= session('sign_user_info');

        if($res == 'GET')
        {
            return view('Home/User/user_address')->with('user_info',$user_info);
        }

        if($res == 'POST')
        {
            if(empty($user_info['id']))
            {
                return ['status'=>0,'info'=>'请登录后修改个人信息'];
            }
            $data = $request->input();
            $data = array_keys($data);
            foreach ($data as $v)
            {
                $re = json_decode($v,true);
                //to do 数据验证
                if(empty($re['address']))
                {
                    return ['status'=>0,'info'=>'请选择正确的收货地址'];
                }
                if(empty($re['address_info']) || mb_strlen($re['address_info'])<2 || mb_strlen($re['address_info'])>100)
                {
                    return ['status'=>0,'info'=>'请输入正确的详细收货地址'];
                }
                if(empty($re['nickname']) || mb_strlen($re['nickname'])<1 || mb_strlen($re['nickname'])>10)
                {
                    return ['status'=>0,'info'=>'请输入正确的收货人姓名'];
                }
                if(empty($re['mobile']))
                {
                    return ['status'=>0,'info'=>'收货人手机号不能为空'];
                }
                preg_match('/^1\d{10}$/',$re['mobile'],$match);
                if(!$match)
                {
                    return ['status'=>0,'info'=>'手机号格式不正确'];
                }

                //将数据插入数据库
                $insert = DB::table('address')->insert([
                    'address'=>$re['address'],
                    'mobile'=>$re['mobile'],
                    'nickname'=>$re['nickname'],
                    'postcode'=>$re['postcode'],
                    'address_info'=>$re['address_info'],
                    'user_id'=>  $user_info['id'],
                ]);
                if($insert)
                {
                    return ['status'=>1,'info'=>'添加成功'];
                }else{
                    return ['status'=>0,'info'=>'添加失败'];
                }

            }
        }
    }

    /**
     * 用户收货地址列表
     */
    public function user_address_list()
    {
        //分配session信息到主页模板
        $user_info= session('sign_user_info');
        $address = DB::table('address')->where(['user_id'=>$user_info['id']])->get();
        return view('Home/User/user_address_list')->with('user_info',$user_info)->with('address',$address);
    }


    /**
     * 用户订单列表
     */
    public function user_order(Request $request)
    {
        //分配session信息到主页模板
        $user_info= session('sign_user_info');
        $res = $request->method();
        if($res == 'GET')
        {
            $order_list = DB::table('order')->where(['user_id'=>$user_info['id']])->get();
            return view('Home/User/user_order')->with('user_info',$user_info)->with('order_list',$order_list);
        }
        if($res == 'POST')
        {
            $data = $request->input();
//            print_r($data);

            $address_id = $data['id'];
            $order_total = $data['order_total'];
            // 数据信息验证 to do
            if(empty($address_id))
            {
                return ['status'=>0,'info'=>'请选择正确的收货地址'];
            }
            if(empty($order_total))
            {
                return ['status'=>0,'info'=>'您还没有选择商品哦'];
            }

            $order_id = DB::table('order')->insertGetId([
                'user_id'=>$user_info['id'],
                'address_id'=>$address_id,
                'pay_status'=>0,
                'order_total'=>$order_total,
            ]);

            $goods = DB::table('shopping_cart')->where(['user_id'=>$user_info['id']])->select('goods_id','property','goods_count')->get();

            $order_goods = [];
            foreach ($goods as $v)
            {
                $v = (array)$v;
                $tmp = [];
                $tmp['order_id'] = $order_id;
                $tmp['goods_id'] = $v['goods_id'];
                $tmp['goods_count'] = $v['goods_count'];
                $tmp['property'] = $v['property'];
                $order_goods[] = $tmp;
            }
            $bool = DB::table('order_goods')->insert($order_goods);
            if($bool)
            {
                $bool1 = DB::table('shopping_cart')->where(['user_id'=>$user_info['id']])->delete();
                if($bool1)
                {
                    return ['status'=>1,'info'=>'订单已生成'];
                }else{
                    return ['status'=>0,'info'=>'订单生成失败'];
                }

            }

        }

    }

    /**
     * 订单购物清单
     */

    public function order_goods(Request $request,$order_id)
    {
        $goods = DB::table('order_goods')
            ->where(['order_id'=>$order_id])
            ->join('goods','order_goods.goods_id','=','goods.goods_id')
            ->join('img','order_goods.goods_id','=','img.goods_id')
            ->get();

        return view('Home/User/order_goods')->with('goods',$goods);
    }

}
